package com.nursinghome.user.controller;

import com.nursinghome.bean.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/user/file")
public class UserFileController {

    @Value("${picdir}")
    private String picdir;

    @PostMapping("/upload")
    public RespBean avatarUpload(MultipartFile pic) throws IOException {
        String filename = pic.getOriginalFilename();
        filename = UUID.randomUUID().toString().replace("-", "") + filename;
        pic.transferTo(new File(picdir + filename));
        return RespBean.ok("上传成功", filename);
    }

}
